<template>
  <div class="order">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row :gutter="10">
        <el-col :span="8">
          <el-input  placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格 -->
       <el-table
        :data="orderList"
        stripe
        border
      >
        <!-- 索引行 -->
        <el-table-column type="index" align="center"></el-table-column>
        <el-table-column
          prop="order_number"
          label="订单编号"
          width="600"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="order_price"
          label="订单价格"
          align="center"
          width="80">
        </el-table-column>
        <el-table-column
          prop="pay_status"
          label="是否付款"
          align="center"
          width="80">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === 0">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
        prop="is_send"
        label="是否发货"
        align="center"
        width="100">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.is_send === '是'">是</el-tag>
            <el-tag type="danger" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
        prop="update_time"
        label="下单时间"
        align="center"
        width="250">
          <template slot-scope="scope">
            {{ scope.row.update_time * 1000 | dataForm}}
          </template>
        </el-table-column>
        <el-table-column
        align="center"
        label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editAddress"></el-button>
            <el-button type="success" icon="el-icon-location-information" size="mini" @click="showProgress"></el-button>
            <el-button type="warning" icon="el-icon-truck" size="mini" @click="getGood(scope.row.order_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="orderList.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="orderList.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 编辑地址弹出框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressDialogVisible"
      width="30%"
      @close="addressClose">
      <!-- 表单 -->
      <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressForm.address1"
            :options="citydata"
            expand-trigger="hover">
          </el-cascader>
        </el-form-item>
         <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 展示物流 -->
     <el-dialog
      title="物流信息"
      :visible.sync="progressVisible"
      class="dialog"
      width="50%">
      <!-- 时间线 -->
       <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          color="#0bbd87"
          :timestamp="activity.ftime">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import citydata from '@/components/order/citydata'
export default {
  data () {
    return {
      orderList: [],
      queryInfo: {
        query: '',
        pagesize: 10,
        pagenum: 1
      },
      total: 0,
      // 编辑地址弹出框显示
      addressDialogVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [{ required: true, message: '请选择省市区', trigger: 'blur' }],
        address2: [{ required: true, message: '请填写详细地址', trigger: 'blur' }]
      },
      citydata,
      // 物流框显示
      progressVisible: false,
      // 物流信息
      progressInfo: []
    }
  },
  created () {
    this.getOrderlist()
  },
  methods: {
    async getOrderlist () {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.orderList = res.data.goods
      this.total = res.data.total
      console.log(this.orderList)
    },
    // 监听 pagesize 改变
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize
      this.getOrderlist()
    },
    // 监听 pagenum
    handleCurrentChange (newnum) {
      this.queryInfo.pagenum = newnum
      this.getOrderlist()
    },
    // 点击编辑地址
    editAddress () {
      this.addressDialogVisible = true
    },
    // 弹出框清空
    addressClose () {
      this.$refs.addressFormRef.resetFields()
    },
    // 点击展示物流框
    showProgress () {
      // 发送请求获取物流信息
      fetch('https://www.liulongbin.top:8888/api/private/v1/kuaidi/1106975712662',
        {
          headers: {
            Authorization: sessionStorage.getItem('token')
          }
        })
        .then(res => res.json())
        .then(res => {
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          this.progressInfo = res.data
          console.log(this.progressInfo)
          this.progressVisible = true
        })
    },
    // 点击发货
    async getGood (id) {
      const result = await this.$confirm('确定发货, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result !== 'confirm') return this.$message.error('已取消发货')
      const { data: res } = await this.$http.put(`orders/${id}`, { is_send: 1, order_pay: 1, order_price: 40 })
      console.log(res)
      if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
      this.getOrderlist()
      this.$message.success('发货成功')
    }
  }
}
</script>

<style scoped lang="less">
.dialog {
  ::v-deep .el-dialog__body {
    height: 500px;
    overflow: hidden;
    overflow-y: auto;
    border-bottom: 1px solid #eee;
    border-top: 1px solid #eee; 
  }
}
</style>
